
import { createReconnectableWebSocket, ReconnectableWebsocket } from "element-sci-ui"
let ws: ReconnectableWebsocket | null = null
const registeredCallbacks: ((data: any) => void)[] = []
ws = createReconnectableWebSocket(`ws://${window.location.host}/api/control`, (data: any) => {
    for (let i = 0; i < registeredCallbacks.length; i++) {
        registeredCallbacks[i](data)
    }
})
// console.log(ws)
export const registerCallback = <T>(callback: (data: T) => void) => {
    registeredCallbacks.push(callback)
}

export const send = (data: string) => {
    ws?.send(data)
}